import { OnInit, Component, ViewChild, Input } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { CourseTypeService } from 'src/app/project/service/icm/icm-base/course-type/course-type.service';
import { CourseTypeDetailComponent } from './course-type-detail.component';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { CourseTypeDTO } from 'src/app/project/model/icm/icm-base/course-type/course-type.dto';
import { ObjectUtil } from 'src/app/project/utils/object-util';
import { TABLE_PARAMETER } from '../../setting/table-parameter';
import { STColumn } from '@delon/abc';
import { CourseService } from 'src/app/project/service/icm/icm-base/course/course.service';

@Component({
  selector: 'app-course-type-list',
  templateUrl: './course-type-list.component.html',
  styles: [],
})
export class CourseTypeListComponent implements OnInit {
  constructor(
    private msg: NzMessageService,
    private courseTypeService: CourseTypeService,
    private courseService: CourseService,
  ) {}

  @ViewChild('courseTypeDetailComponent', { static: false })
  courseTypeDetailComponent: CourseTypeDetailComponent;

  /**
   * 分页参数初始化
   */
  queryOptions: QueryOptions = {
    page: 0,
    size: 20,
    sort: 'id,asc',
  };

  /**
   * 查询参数
   */
  @Input()
  queryParams;

  /**
   * 请求数据标识
   */
  loading = false;

  /**
   * 表格数据
   */
  tableData: Array<CourseTypeDTO> = [];

  /**
   * 列表参数
   */
  TABLE_PARAMETER = ObjectUtil.deepClone(TABLE_PARAMETER);

  /**
   * 表格的列定义
   */
  stColumns: STColumn[] = [
    {
      title: '序号',
      render: 'number',
      width: '2%',
      className: 'text-center',
    },
    {
      title: '课程详细类型编号',
      index: 'code',
      width: '4%',
      className: 'text-left',
    },
    {
      title: '课程详细类型名称',
      index: 'name',
      width: '4%',
      className: 'text-left',
    },
    {
      title: '课程基础类型',
      render: 'courseBaseType',
      width: '4%',
      className: 'text-left',
      default: '无',
    },
    {
      title: '显示顺序',
      index: 'showOrder',
      width: '3%',
      className: 'text-center',
      sort: this.TABLE_PARAMETER.sortDef,
    },
    {
      title: '操作',
      render: 'operations',
      index: 'operation',
      width: '4%',
      className: 'text-center',
      fixed: 'right',
      acl: ['ROLE_SENATOR'],
    },
  ];

  /**
   * 表格变化事件，用于双击、排序，翻页等操作
   * @param e event
   */
  change(e: any): void {
    // 双击事件
    if (e.type === 'dblClick') {
      this.loading = true;
      // tslint:disable-next-line: deprecation
      this.courseTypeService.findById(e.dblClick.item.id).subscribe(
        data => {
          this.courseTypeDetailComponent.edit(true, data);
        },
        null,
        () => {
          this.loading = false;
        },
      );
    }
    // 排序事件
    if (e.type === 'sort') {
      if (e.sort.map) {
        const temp = e.sort.map.sort;
        if (temp) {
          this.queryOptions.sort = temp.replace(/[\.-]/g, ',');
        }
      }
      this.loadData();
    }
    // 翻页设置
    if (e.type === 'ps' || e.type === 'pi') {
      this.queryOptions.page = e.pi - 1;
      this.queryOptions.size = e.ps;
      this.loadData();
    }
  }

  /**
   * 获取课程类型数据
   */
  loadData() {
    this.loading = true;
    this.courseTypeService
      .findOnePage(this.queryOptions, this.queryParams.code, this.queryParams.name, this.queryParams.courseBaseType)
      // tslint:disable-next-line: deprecation
      .subscribe(
        data => {
          if (data) {
            this.tableData = data.data;
            this.TABLE_PARAMETER.page.total = data.totalRecords;
          }
        },
        null,
        () => {
          this.loading = false;
        },
      );
  }

  /**
   * 添加课程类型
   */
  create() {
    this.courseTypeDetailComponent.edit(false, null);
  }

  /**
   * 修改课程类型信息
   * @param row 课程类型数据
   */
  onUpdate(row: CourseTypeDTO) {
    this.loading = true;
    // tslint:disable-next-line: deprecation
    this.courseTypeService.findById(row.id).subscribe(
      data => {
        this.courseTypeDetailComponent.edit(false, data);
      },
      null,
      () => {
        this.loading = false;
      },
    );
  }

  /**
   * 删除课程类型
   */
  onDelete(row: CourseTypeDTO) {
    // 判断该课程类型是否存在课程引用
    this.courseService.findOnePage(this.queryOptions, null, null, row.id, null).subscribe(data => {
      if (data) {
        if (data.data.length !== 0) {
          this.msg.error('删除失败');
          this.msg.error('该课程类型尚存在课程使用');
        } else {
          this.courseTypeService.delete(row.id).subscribe(
            () => {
              this.msg.success('删除成功');
              this.loadData();
            },
            () => this.msg.error('删除失败'),
            () => this.loadData(),
          );
        }
      }
    });
  }

  ngOnInit() {
    this.loadData();
  }
}
